package conf

import (
	"log"

	"gopkg.in/ini.v1"

	"gitee.com/daonna/collab-manage-system/repository/db"
	"gitee.com/daonna/collab-manage-system/repository/redis"
)

var (
	// 服务配置
	AppMode  string
	HttpPort string

	// MySQL配置
	DB         string
	DbHost     string
	DbPort     string
	DbUser     string
	DbPassword string
	DbName     string

	// LMS配置
	ApiRoot        string
	MsgUrl         string
	InventoryCheck bool
	Proxy          bool
	LmsUrl         string
	UrlHkProxy     string
	UrlChangePwd   string
	LmsUser        string
	LmsPassword    string
	LmsClient      string
	NewPwd         string
	MaxQuantity    int
	Account        string
	Broker         string

	// 缓存路径配置
	LinuxMsgCache   string
	LinuxFeaCache   string
	WindowsMsgCache string
	WindowsFeaCache string

	// SBL数据库配置
	SblUser     string
	SblPassword string
	SblHost     string
	SblPort     string
	SblDatabase string
	SblCharset  string

	// Inventory数据库配置
	InvUser     string
	InvPassword string
	InvHost     string
	InvPort     string
	InvDatabase string
	InvCharset  string

	// 股票Redis配置
	RedisHost string
	RedisPort string

	// 交易服务器配置
	TradeServerHost string
	TradeServerPort string

	// User数据库配置
	UserUser     string
	UserPassword string
	UserHost     string
	UserPort     string
	UserDatabase string
	UserCharset  string

	// UserRole数据库配置
	UserRoleUser     string
	UserRolePassword string
	UserRoleHost     string
	UserRolePort     string
	UserRoleDatabase string
	UserRoleCharset  string

	// Role数据库配置
	RoleUser     string
	RolePassword string
	RoleHost     string
	RolePort     string
	RoleDatabase string
	RoleCharset  string
)

func Init() {
	user, err := db.LoadConfig("./conf/config.ini")
	if err != nil {
		log.Fatalf("Failed to load database config: %v", err)
	}

	// 初始化数据库连接
	if err := db.InitDBClient(user); err != nil {
		log.Fatalf("Failed to initialize database connections: %v", err)
	}
	file, err := ini.Load("./conf/config.ini")
	if err != nil {
		panic(err)
	}
	// 加载Redis配置
	LoadRedisConfig(file)

	// 初始化Redis连接
	if err := redis.InitRedisClients(RedisHost, RedisPort); err != nil {
		log.Fatalf("Failed to initialize Redis clients: %v", err)
	}

	LoadService(file)
	LoadMysql(file)
	LoadLMS(file)
	LoadCache(file)
	LoadDbSbl(file)
	LoadDbInventory(file)
	LoadTradeServer(file)
	LoadDbUser(file)
	LoadDbUserRole(file)
	LoadDbRole(file)

}

func LoadRedisConfig(file *ini.File) {
	// 加载股票实盘Redis配置
	RedisHost = file.Section("redis").Key("Host").String()
	RedisPort = file.Section("redis").Key("Port").String()
	log.Printf("Loaded  Redis config - Host: %s, Port: %s", RedisHost, RedisPort)
}

func LoadService(file *ini.File) {
	AppMode = file.Section("service").Key("AppMode").String()
	HttpPort = file.Section("service").Key("HttpPort").String()
}

func LoadMysql(file *ini.File) {
	DB = file.Section("mysql").Key("DB").String()
	DbHost = file.Section("mysql").Key("DbHost").String()
	DbPort = file.Section("mysql").Key("DbPort").String()
	DbUser = file.Section("mysql").Key("DbUser").String()
	DbPassword = file.Section("mysql").Key("DbPassword").String()
	DbName = file.Section("mysql").Key("DbName").String()
}

func LoadLMS(file *ini.File) {
	ApiRoot = file.Section("lms").Key("ApiRoot").String()
	MsgUrl = file.Section("lms").Key("MsgUrl").String()
	InventoryCheck = file.Section("lms").Key("InventoryCheck").MustBool()
	Proxy = file.Section("lms").Key("Proxy").MustBool()
	LmsUrl = file.Section("lms").Key("Url").String()
	UrlHkProxy = file.Section("lms").Key("UrlHkProxy").String()
	UrlChangePwd = file.Section("lms").Key("UrlChangePwd").String()
	LmsUser = file.Section("lms").Key("User").String()
	LmsPassword = file.Section("lms").Key("Password").String()
	LmsClient = file.Section("lms").Key("Client").String()
	NewPwd = file.Section("lms").Key("NewPwd").String()
	MaxQuantity = file.Section("lms").Key("MaxQuantity").MustInt()
	Account = file.Section("lms").Key("Account").String()
	Broker = file.Section("lms").Key("Broker").String()
}

func LoadCache(file *ini.File) {
	LinuxMsgCache = file.Section("cache").Key("LinuxMsgCache").String()
	LinuxFeaCache = file.Section("cache").Key("LinuxFeaCache").String()
	WindowsMsgCache = file.Section("cache").Key("WindowsMsgCache").String()
	WindowsFeaCache = file.Section("cache").Key("WindowsFeaCache").String()
}

func LoadDbSbl(file *ini.File) {
	SblUser = file.Section("db_sbl").Key("User").String()
	SblPassword = file.Section("db_sbl").Key("Password").String()
	SblHost = file.Section("db_sbl").Key("Host").String()
	SblPort = file.Section("db_sbl").Key("Port").String()
	SblDatabase = file.Section("db_sbl").Key("Database").String()
	SblCharset = file.Section("db_sbl").Key("Charset").String()
}

func LoadDbInventory(file *ini.File) {
	InvUser = file.Section("db_inventory").Key("User").String()
	InvPassword = file.Section("db_inventory").Key("Password").String()
	InvHost = file.Section("db_inventory").Key("Host").String()
	InvPort = file.Section("db_inventory").Key("Port").String()
	InvDatabase = file.Section("db_inventory").Key("Database").String()
	InvCharset = file.Section("db_inventory").Key("Charset").String()
}

func LoadDbUser(file *ini.File) {
	UserUser = file.Section("db_user").Key("User").String()
	UserPassword = file.Section("db_user").Key("Password").String()
	UserHost = file.Section("db_user").Key("Host").String()
	UserPort = file.Section("db_user").Key("Port").String()
	UserDatabase = file.Section("db_user").Key("Database").String()
	UserCharset = file.Section("db_user").Key("Charset").String()
}

func LoadDbUserRole(file *ini.File) {
	UserRoleUser = file.Section("db_user_role").Key("User").String()
	UserRolePassword = file.Section("db_user_role").Key("Password").String()
	UserRoleHost = file.Section("db_user_role").Key("Host").String()
	UserRolePort = file.Section("db_user_role").Key("Port").String()
	UserRoleDatabase = file.Section("db_user_role").Key("Database").String()
	UserRoleCharset = file.Section("db_user_role").Key("Charset").String()
}

func LoadDbRole(file *ini.File) {
	RoleUser = file.Section("db_role").Key("User").String()
	RolePassword = file.Section("db_role").Key("Password").String()
	RoleHost = file.Section("db_role").Key("Host").String()
	RolePort = file.Section("db_role").Key("Port").String()
	RoleDatabase = file.Section("db_role").Key("Database").String()
	RoleCharset = file.Section("db_role").Key("Charset").String()
}

// LoadTradeServer 加载交易服务器配置
func LoadTradeServer(file *ini.File) {
	TradeServerHost = file.Section("trade_server").Key("host").String()
	TradeServerPort = file.Section("trade_server").Key("port").String()
}
